package com.yzjs.localhost.service.sysMenu.impl;

import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dictionaries.NeusoftDataStatus;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.sysDept.SysDeptEntity;
import com.yzjs.localhost.entity.sysMenu.SysMenuEntity;
import com.yzjs.localhost.dao.sysMenu.SysMenuMapper;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.entity.tNeusoftSysDept.TNeusoftSysDeptEntity;
import com.yzjs.localhost.entity.tNeusoftSysMenu.TNeusoftSysMenuEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.sysMenu.SysMenuService;
import com.yzjs.localhost.service.tNeusoftSysMenu.TNeusoftSysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("sysMenuService")
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenuEntity, SysMenuMapper > implements SysMenuService {


    @Autowired
    private TNeusoftSysMenuService  neusoftSysMenuService;
    /**
     * 获取菜单信息
     * @param dto
     * @param rights
     * @return
     * @throws Exception
     */
    @Override
    public ComparisonResultDTO MenuCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce =dto.getBeforce();
        String beforceInfo  = beforce.getInfo();
        Map<String,Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo  = after.getInfo();

        ComparisonResultDTO resultDTO = null ;
        if (beforce.getOperatorCode().contains("menu:add")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 菜单图标  显示顺序 菜单名称 路由地址 菜单类型
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"icon,orderNum,menuName,path,menuType");
        }else if (beforce.getOperatorCode().contains("menu:edit")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 菜单ID  菜单图标 显示顺序  菜单名称  路由地址 菜单类型
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"menuId,icon,orderNum,menuName,path,menuType");
        } else if (beforce.getOperatorCode().contains("menu:remove")){
            beforceParse.put("delFlag","2");
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 菜单ID 状态
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"menuId,delFlag");
            if (null==afterParse.get("menuId")){
                resultDTO.setAbstractName("菜单ID为空");
                resultDTO.setWarningInfo("菜单ID为空");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            SysMenuEntity  menuEntity   = new SysMenuEntity();
            menuEntity.setMenuId(Long.valueOf((String) afterParse.get("menuId")));
            menuEntity.setDelFlag((String) afterParse.get("delFlag"));
            menuEntity.setDataIdentification(after.getDataIdentification());
            this.insert(menuEntity);
            return resultDTO;
        }
        // 记录表表信息
        SysMenuEntity menuEntity    = JsonUtil.json2pojo(after.getInfo(), SysMenuEntity.class);
        menuEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        menuEntity.setDataIdentification(after.getDataIdentification());
        this.insert(menuEntity);
        //菜单信息更新
        SynchronizationSysMenu(menuEntity,beforce.getOperatorCode());

        return resultDTO;
    }


    /**
     * 同步菜单信息，东软接收的部门数据进行更新本地权限
     * @param menuEntity
     * @param operatorCode
     */
    @Override
    public void SynchronizationSysMenu(SysMenuEntity menuEntity, String operatorCode) {
        TNeusoftSysMenuEntity  neusoftSysMenu  = new TNeusoftSysMenuEntity();
        neusoftSysMenu.setMenuId(menuEntity.getMenuId());
        TNeusoftSysMenuEntity resultMenu = neusoftSysMenuService.findOne(neusoftSysMenu);
        if(resultMenu == null){
            BeanUtils.copyProperties(menuEntity,neusoftSysMenu);
            neusoftSysMenuService.insert(neusoftSysMenu);
        } else {
            BeanUtils.copyProperties(menuEntity,resultMenu);
            neusoftSysMenuService.update(resultMenu);
        }
        return;
    }
}